<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
        }

        /* 设整个内容的大小，居中 */
        .container {
            width: 400px;
            margin: 50px auto;
        }

        /* 给h1加一个下边距 */
        .container h1 {
            margin: 22px 0;
        }

        /* 输入框和下面内容的距离 */
        #shopping-form {
            margin-bottom: 30px;
        }

        /* #shopping-form label {
            display: block;
            font-weight: 500;
        } */

        /* 设输入框和btn为弹性盒模型 并设上边距*/
        .add-box {
            display: flex;
            margin-top: 10px;
        }

        /* 输入框的设置 */
        #item-name {
            width: 250px;
            padding: 12px 8px;
            background: #f2ecee;
            border: none;
            border-left: 7px solid #d9cfd3;
            border-radius: 3px;
            cursor: pointer;
            /* 变小手 */
            font-weight: 600;
            text-align: center;
            /* 文字居中 */
            font-style: 16px;
            outline: none;
            flex-grow: 1;
        }

        /* 输入框内字体颜色 */
        ::placeholder {
            color: #d9cfd3;
        }

        /* 设置添加按钮 */
        .btn {
            font-style: 14px;
            font-weight: 600;
            padding: 10px 30px;
            border-radius: 3px;
            /* 边框圆化 */
            border: none;
            cursor: pointer;
            transition: background-color 0.2s ease;
            /* 设置触碰时颜色转换时间 */
            margin-left: 10px;
        }

        #btn-add {
            color: #fff;
            background-color: #1c6c9e;
        }

        /* 触碰添加按钮时颜色转换 */
        #btn-add:hover {
            background-color: #1d465f;
        }

        /* 输入框左边框的颜色 */
        #item-name:focus {
            border-color: #458179;
        }

        /* 内容的设置 */
        #shopping-list li {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        /* 设置内容和删除按钮 */
        #shopping-list p {
            text-align: 10px;
            flex-grow: 1;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>购物清单添加/删除</h1>
        <div id="shopping-form">
            <label for="item-name">消费名称</label>
            <div class="add-box">
                <input id="item-name" type="text" placeholder="输入购物名称">
                <button id="btn-add" class="btn">添加</button>
            </div>
        </div>

        <ul id="shopping-list">
            <li>
                <p>123456</p>
                <button class="btn">删除</button>
            </li>
        </ul>
    </div>

    <script>
        const itemName = document.querySelector('#item-name')
        const btnAdd = document.querySelector('#btn-add')
        const shoppingList = document.querySelector('#shopping-list')

        btnAdd.onclick = () => {
            shoppingList.insertAdjacentHTML('beforeend', `
            <li>
                <p>${itemName.value}</p>
                <button class="btn">删除</button>
            </li>
            `)
            itemName.value = ''

            // const btns = document.querySelectorAll('#shopping-list .btn')
            // for (let i = 0; i < btns.length; i++) {
            //     btns[i].onclick = function () {
            //         this.parentNode.remove()
            //     }
            // }

            shoppingList.onclick = function (e) {
                if (e.target.tagName === 'BUTTON') {
                    e.target.parentNode.remove()
                }
            }
        }

    </script>
</body>

</html>